<?php

namespace app\web\controller;
use app\web\model\CashModel;
use app\adminmall\model\BrandModel;
use app\web\model\LineGoodsModel;
use app\web\model\StoreGoodsTypeModel;
use app\web\model\LineStocklogModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 库存
 */
class StockController extends StoreBaseController{

 	protected $model;	
	protected $storeGoodsType_model;
	protected $brand_model;
	protected $stocklogmodel;

    protected function _initialize() {
    	parent::_initialize();
		$this->model = new LineGoodsModel();
		//商品分类
		$this->storeGoodsType_model = new StoreGoodsTypeModel();
		//品牌
		$this->brand_model = new BrandModel();
		$this->stocklogmodel = new LineStocklogModel();

	}
	
	public function manage(){

		$where = null;	
		if(request()->isPost()){
		 	$param = input('post.');
			//商品名称
			if($param['goods_name']){
				$where['g.goods_name'] = ['like', '%'.$param['goods_name'].'%'];
			}
			//商品分类
			if($param['goods_typeid'] != 0){
				$where['g.goods_typeid'] = $param['goods_typeid'];
			}		
						
			//状态
			if($param['status'] == 1){
				$where['g.stock - g.stock_warning'] = ['>',0];					
			}else if($param['status'] == 2){
				$where['g.stock - g.stock_warning'] = ['<=',0];
			}
		}
		
		
		$where['store_id'] = session('storeInfo')['id'];
		$list=Db::name("line_goods")->alias('g')
		->join('mall_store_goodstype t','t.id=g.goods_typeid','LEFT')
		->field('g.*,t.name')->where($where)->order("g.add_time desc")->paginate(10);
//		var_dump($list);exit;	
		
		$storeGoodsType_list = $this->storeGoodsType_model->where('agent_id',session('storeInfo')['id'])->select();	   	
		$brand_list = $this->brand_model->select();
		$this->assign('brand_list',$brand_list);
		$this->assign('storeGoodsType_list',$storeGoodsType_list);
        return $this->fetch('manage', ['list' => $list]);
	}

	
	/**
	 * 修改库存预警
	 */
	public function saveyj(){
    	$param = input('post.');		
		$id = input('id');
		$param['stock_warning'] = input('stock_warning');
		if ($this->model->allowField(true)->save($param,$id) !== false) {
			return json(['code'=>200,'msg'=>"修改成功"]);
		}else{
			return json(['code'=>408,'msg'=>"修改失败"]);
		}
	}

	
	public function stockoper(){
		$param = input('post.');
		$type = input('type');
		if($type == 1){
			$sn = "IN".order_create_no();
		}else{
			$sn = "OUT".order_create_no();
		}		
		$add_time = now_time();
		
		$store = session('storeInfo');
		$employee=session('employee');
		if($employee){
			$param['mobile'] = $employee['mobile'];//员工手机
		}else{
			$param['mobile'] = $store['store_tel'];//管理员手机
		}
		$id = input('id');
		$name = input('name');
		
		$goodsinfo = $this->model->where('store_id',session('storeInfo')['id'])->find($id);
		$stock = $goodsinfo['stock'];
		
		$this->assign('type',$type);
		$this->assign('sn',$sn);
		$this->assign('add_time',$add_time);
		$this->assign('czy',$param['mobile']);
		$this->assign('id',$id);
		$this->assign('name',$name);
		$this->assign('stock',$stock);
		return $this->fetch();
	}

	/**
	 * 保存出入库修改记录
	 */	
	public function savecrkc(){
		$store = session('storeInfo');
		$param = input();
		$param['store_id'] = $store['id'];
		$param['store_name'] = $store['store_name'];
		$goods_id = input('goods_id');
		$type = input('type');
		$stock = input('stock');
		
		if ($this->stocklogmodel->allowField(true)->save($param) !== false) {
			if($goods_id){
				if($type==1){
					Db::name('line_goods')->where('id',$goods_id)->setInc('stock', input('stock'));
				}else{
					Db::name('line_goods')->where('id',$goods_id)->setDec('stock', input('stock'));
				}
			}			
			return json(['code'=>200,'msg'=>"操作成功"]);
		}else{
			return json(['code'=>408,'msg'=>"操作失败"]);
		}
	}
	
		
	/**
	 * 入库查询
	 */
	public function storage(){
		$where = null;	
        //搜索一栏
		if(request()->isPost()){
		 	$param = input('post.');
			//入库单号
			if($param['log_sn']){
				$where['sn'] = ['like', '%'.$param['log_sn'].'%'];
			}
			//入库类型
			if($param['stocktype'] != 0){
				$where['stocktype'] = $param['stocktype'];
			}
			//有效期
			if($param['start_time']&&$param['end_time']){
				$where['add_time'] = ['between time', [$param['start_time'],$param['end_time']]];
			}			
		}
    	$where['store_id'] = session('storeInfo')['id'];
		$where['type'] = 1;
        $list = $this->stocklogmodel->field('id,sn,add_time,stocktype,type,czy')->where($where)->order('add_time desc')->group('sn,type')->paginate(10);
        return $this->fetch('storage', ['list' => $list]);
	}
	
	/**
	 * 出库查询
	 */
	public function placing(){
		$where = null;	
        //搜索一栏
		if(request()->isPost()){
		 	$param = input('post.');
			//入库单号
			if($param['log_sn']){
				$where['sn'] = ['like', '%'.$param['log_sn'].'%'];
			}
			//入库类型
			if($param['stocktype'] != 0){
				$where['stocktype'] = $param['stocktype'];
			}
			//有效期
			if($param['start_time']&&$param['end_time']){
				$where['add_time'] = ['between time', [$param['start_time'],$param['end_time']]];
			}			
		}
    	$where['store_id'] = session('storeInfo')['id'];
		$where['type'] = 2;
        $list = $this->stocklogmodel->field('id,sn,add_time,stocktype,type,czy')->where($where)->order('add_time desc')->group('sn,type')->paginate(10);
        return $this->fetch('placing', ['list' => $list]);
	}
	
	/**
	 * 出入库明细查询
	 */
	public function detail(){
		$where = null;
		$id = input('id');
        //搜索一栏
		if(request()->isPost()){
		 	$param = input('post.');
			//入库单号
			if($param['log_sn']){
				$where['sn'] = ['like', '%'.$param['log_sn'].'%'];
			}
			//商品名称
			if($param['name']){
				$where['goods_name'] = $param['name'];
			}		
		}
    	$where['store_id'] = session('storeInfo')['id'];
		if($id){
			$where['goods_id'] = $id;
		}
        $list = $this->stocklogmodel->where($where)->order('add_time desc')->paginate(10);
        return $this->fetch('detail', ['list' => $list]);
	}
	
	/**
	 * 制作出入库单
	 */
	public function stockmake($type){
		$param = input('post.');
		if($type == 1){
			$sn = "IN".order_create_no();
		}else{
			$sn = "OUT".order_create_no();
		}		
		$add_time = now_time();
		
		$store = session('storeInfo');
		$employee=session('employee');
		if($employee){
			$param['mobile'] = $employee['mobile'];//员工手机
		}else{
			$param['mobile'] = $store['store_tel'];//管理员手机
		}
		$id = input('id');
		$name = input('name');
		$stock = input('stock');
		
		
		$this->assign('type',$type);
		$this->assign('sn',$sn);
		$this->assign('add_time',$add_time);
		$this->assign('czy',$param['mobile']);
		$this->assign('id',$id);
		$this->assign('name',$name);
		$this->assign('stock',$stock);
		return $this->fetch();
	}
	
	 /**
     * 商品管理
     * @author [lx] 
     */
    public function goods_list( $page = 1){
    	$field = 'g.*,t.name';
    	if(request()->isPost()){
		 	$param = input('post.');
			//商品编码
			if($param['goods_no']){
				$where['g.goods_no'] = ['like', '%'.$param['goods_no'].'%'];
			}	
			//商品名称
			if($param['goods_name']){
				$where['g.goods_name'] = ['like', '%'.$param['goods_name'].'%'];
			}			
	 	}		
		$where['g.store_id'] = session('storeInfo')['id'];
		$list=Db::name("line_goods")->alias('g')
		->join('mall_store_goodstype t','t.id=g.goods_typeid','LEFT')
		->field($field)->where($where)->order("g.add_time desc")->paginate(10);
        return $this->fetch('goods_list', ['list' => $list]);
    }
	
	/**
	 * 批量出入库
	 */
	public function savestock(){
		$store = session('storeInfo');
		$param = input();
		$goodsid = $param['goodsid'];
		$goods_names = $param['goods_names'];
		$goods_stock = $param['goods_stock'];
		for ($i=0; $i < count($goodsid); $i++) {
			$list[] = ['sn'=>$param['sn'],'store_id'=>$store['id'],'store_name'=>$store['store_name']
			,'add_time'=>$param['add_time'],'stocktype'=>$param['stocktype'],'type'=>$param['type']
			,'comments'=>$param['comments'],'czy'=>$param['czy'],'jbrname'=>$param['jbrname']
			,'goods_id'=>$goodsid[$i],'goods_name'=>$goods_names[$i],'stock'=>$param['stock_'.$goodsid[$i]]
			,'comment'=>$param['comment_'.$goodsid[$i]]];
			
			$list1[] = ['id'=>$goodsid[$i],'stock'=>$goods_stock[$i] + $param['stock_'.$goodsid[$i]]];
			$list2[] = ['id'=>$goodsid[$i],'stock'=>$goods_stock[$i] - $param['stock_'.$goodsid[$i]]];
			if(!$param['stock_'.$goodsid[$i]]){
				$this->error('出库库存不能为空,且只能为1-1000000的整数');
			}
			if($param['type']==2){
				if($param['stock_'.$goodsid[$i]]>$goods_stock[$i]){
					$this->error('库存不足');
				}
			}
		}
		if($param['type']==1){
			$true1 = $this->model->allowField(true)->saveAll($list1);
		}else if($param['type']==2){
			$true2 = $this->model->allowField(true)->saveAll($list2);	
		}
		$true = $this->stocklogmodel->allowField(true)->saveAll($list);
		if($true){
			if($param['type']==1){
				$this->success('添加成功', url('stock/storage'));
			}else{
				$this->success('添加成功', url('stock/placing'));
			}		
		}else{
			$this->error('添加失败');
		}
		
		return $this->fetch();
	}

	 /**
     * 出入库明细
     * @author [lx] 
     */
    public function stocklist( $page = 1){
    	$param = input('post.');
		$type = input('type');
		$sn = input('sn');
		$id = input('id');
		$stockinfo = $this->stocklogmodel->find($id);		
		$stocklist = $this->stocklogmodel->where('sn',$sn)->select();
		$this->assign('type',$type);
		$this->assign('info',$stockinfo);
		$this->assign('goodslist',$stocklist);
		return $this->fetch();
	}
	
	
	/**
	 * 库存盘点
	 */
	public function index(){
		$map=null;	
        //搜索一栏
		if(request()->isPost()){
		 	$param = input('post.');
			//订单号
			if($param['sn']){
				$map['sn'] = ['like', '%'.$param['sn'].'%'];
			}
			//操作员
			if($param['czy'] != 0){
				$map['czy'] = $param['czy'];
			}
			//状态
			if($param['status']){
				$map['status'] = ['like', '%'.$param['status'].'%'];			
			}		
		}
		$list = db('line_stock')->where($map)->paginate(10);
        return $this->fetch('index', ['list' => $list]);
	}
	
	/**
	 * 添加库存
	 */
	public function add(){
		if(request()->isPOST()){
        	$param=input('post.');
			$param['update_time'] = now_time();
			$param['store_name'] = session('storeInfo')['store_name'];
			$param['czy'] = session('storeInfo')["store_tel"];
			$res=db('line_stock')->insert($param);			
			if ($res) {
                $this->success('添加成功', url('index'));
            } else {           	
                $this->error('添加失败');
            }
       }
      return $this->fetch();
	}
	/**
	 * 編輯库存
	 */
	public function edit(){
		$where['id'] = input('param.id');
		if(request()->ispost()){
            $param = input('post.');         
         	if (db('line_stock')->where($where)->update($param) !== false) {
                $this->success('修改成功', url('index'));
            } else {
                $this->error('修改失败');
            }
        }
        $this->assign('list',db('line_stock')->where($where)->find());
        return $this->fetch();
	}
	/**
     * [del删除]
     * @author [] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->storemenu_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	/**
	 * 商品操作日志
	 */
	public function log(){
		echo "快速消费成功";
		exit;
	}
	
}